//
// Created by antler on 2018/10/31.
//

#include "ShellUtils.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

namespace ShellUtils {
    int runShellCommandR(const char *command, std::string *pOut) {
        FILE *f = popen(command, "r");
        if (NULL == f)
            return -1;

        int ret = -1;

        if (pOut != NULL) {
            char buf[256] = {};
            size_t read_ret = 0;
            while ((read_ret = fread(buf, sizeof(char), sizeof(buf), f)) > 0) {
                pOut->append(buf, read_ret);
            }
            ret = 0;
        } else {
            ret = 0;
        }

        pclose(f);

        return ret;
    }

    int runShellCommand(const char *command) {
        return runShellCommandR(command, nullptr);
    }
};